﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int i;
        int Ovin = 0; 
        int Xvin = 0; 
        string[] arr = new string[10]; //задание поля игры
        string t = "X";
       
 
        public Form1()
        {
            InitializeComponent();
        }

        public string Game(int j) //чередование ходов, "О" после "Х"
        {
            string faket;
            if (i % 2 == 1)
            {
                t = "X";
                faket = "O";
            }
            else { 
                t = "O";
                faket = "X";
            }

            arr[j] = t;
            i += 1;

            label1.Text = faket;
            
          
            return t;

        }

        public void who_vin() //проверка условий победы
        {
            
            if ((arr[1] == "X" & arr[2] == "X" & arr[3] == "X") | (arr[1] == "X" & arr[5] == "X" & arr[9] == "X") | (arr[1] == "X" & arr[4] == "X" & arr[7] == "X") | (arr[2] == "X" & arr[5] == "X" & arr[8] == "X") | (arr[3] == "X" & arr[6] == "X" & arr[9] == "X") | (arr[3] == "X" & arr[5] == "X" & arr[7] == "X") | (arr[4] == "X" & arr[5] == "X" & arr[6] == "X") | (arr[7] == "X" & arr[8] == "X" & arr[9] == "X"))
            {
                Xvin += 1; //плюс один к количеству побед
                label3.Text = "Текущий счет: \n[X]    " + Xvin.ToString() + " : " + Ovin.ToString() + "    [O]"; //отображение текущего счета
                MessageBox.Show(" Игрок X победил "); //вывод диалогового окна
                refresch();
                enabbut();
            }

            if ((arr[1] == "O" & arr[2] == "O" & arr[3] == "O") | (arr[1] == "O" & arr[5] == "O" & arr[9] == "O") | (arr[1] == "O" & arr[4] == "O" & arr[7] == "O") | (arr[2] == "O" & arr[5] == "O" & arr[8] == "O") | (arr[3] == "O" & arr[6] == "O" & arr[9] == "O") | (arr[3] == "O" & arr[5] == "O" & arr[7] == "O") | (arr[4] == "O" & arr[5] == "O" & arr[6] == "O") | (arr[7] == "O" & arr[8] == "O" & arr[9] == "O"))
            {
                Ovin += 1;
                label3.Text = "Текущий счет: \n[X]    " + Xvin.ToString() + " : " + Ovin.ToString() + "    [O]";
                MessageBox.Show(" Игрок O победил ");
                refresch();
                enabbut();
            }
            if ((arr[1] != "") & (arr[2] != "") & (arr[3] != "") & (arr[4] != "") & (arr[5] != "") & (arr[7] != "") & (arr[8] != "") & (arr[9] != ""))
            {
               
                
                MessageBox.Show(" Ничья ");
                refresch();
                enabbut();
            }
           
        }
        
        public void refresch() //обновление поля (возвращение к начальному состоянию)
        {
            button1.Text = "";
            button2.Text = "";
            button3.Text = "";
            button4.Text = "";
            button5.Text = "";
            button6.Text = "";
            button7.Text = "";
            button8.Text = "";
            button9.Text = "";
            label1.Text = "X";
            for (i = 0; i < 10; i += 1)
            {
                arr[i] = "";
            }
            i = 1;

        }

         public void enabbut() //деактивация поля
        {
            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;
            button7.Enabled = false;
            button8.Enabled = false;
            button9.Enabled = false;
            toolStripMenuItem1.Enabled = true;
            toolStripMenuItem2.Enabled = false;
            label1.Visible = false;
        }

        //проверка на ниличие в клетке символа
        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "X" | button1.Text == "O")
            {
                
            }
            else { button1.Text = Game(1); who_vin(); }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "X" | button2.Text == "O")
            {
                
            }
            else { button2.Text = Game(2); who_vin(); }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (button3.Text == "X" | button3.Text == "O")
            {
                
            }
            else { button3.Text = Game(3); who_vin(); }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (button4.Text == "X" | button4.Text == "O")
            {
                
            }
            else { button4.Text = Game(4); who_vin(); }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (button5.Text == "X" | button5.Text == "O")
            {
                
            }
            else { button5.Text = Game(5); who_vin(); }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (button6.Text == "X" | button6.Text == "O")
            {
                
            }
            else { button6.Text = Game(6); who_vin(); }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (button7.Text == "X" | button7.Text == "O")
            {
                
            }
            else { button7.Text = Game(7); who_vin(); }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (button8.Text == "X" | button8.Text == "O")
            {
                
            }
            else { button8.Text = Game(8); who_vin(); }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (button9.Text == "X" | button9.Text == "O")
            {
               
            }
            else { button9.Text = Game(9); who_vin(); }
        }


       private void Form1_Load(object sender, EventArgs e) //отображение текущего счета
        {
            refresch();
            label3.Text = "Текущий счет: \n[X]    " + Xvin.ToString() + " : " + Ovin.ToString() + "    [O]";
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e) 
        {
            button1.Enabled = true; //активация поля
            button2.Enabled = true;
            button3.Enabled = true;
            button4.Enabled = true;
            button5.Enabled = true;
            button6.Enabled = true;
            button7.Enabled = true;
            button8.Enabled = true;
            button9.Enabled = true;
            toolStripMenuItem1.Enabled = false;
            toolStripMenuItem2.Enabled = true;
            toolStripDropDownButton2.Enabled = true;
            label3.Visible = true;
            label1.Visible = true;
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            refresch();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            Close(); 
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            Xvin = 0;
            Ovin = 0;
            label3.Text = "Текущий счет: \n[X]    " + Xvin.ToString() + " : " + Ovin.ToString() + "    [O]";
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Игроки по очереди ставят на свободные клетки поля 3х3 знаки (один всегда крестики, другой всегда нолики). Первый, выстроивший в ряд 3 своих фигуры по вертикали, горизонтали или диагонали, выигрывает. Первый ход делает игрок, ставящий крестики.");
        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Логическая игра «Крестики-нолики»\nРазработал: Соболев А.А. 8B13\nТомский Политехнический университет\n2013 г.");
        }



    }
}
